package com.chenjiagou.controller;

import com.chenjiagou.feign.FlightFeignClient;
import com.chenjiagou.feign.NotificationFeignClient;
import com.chenjiagou.pojo.Flight;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/admin/notification")
public class AdminNotificationController {

    @Autowired
    private NotificationFeignClient notificationFeignClient;
    @Autowired
    private FlightFeignClient flightFeignClient;



    // 提交发布通知
    @PostMapping("/publish")
    public String publishNotification(@RequestParam("flightId") int flightId,
                                      @RequestParam("title") String title,
                                      @RequestParam("content") String content,
                                      HttpSession session) {
        Integer role = (Integer) session.getAttribute("role");
        if (role == null || role != 1) {
            return "redirect:/login";
        }

        notificationFeignClient.publishNotification(flightId, title, content);
        return "redirect:/admin/notification/manage?success=1";
    }


    @GetMapping("/manage")
    public String showManagePage(HttpSession session, Model model) {
        Integer role = (Integer) session.getAttribute("role");
        if (role == null || role != 1) {
            return "redirect:/login";
        }

//        List<Flight> flights = flightFeignClient.getAllFlights(); // 新增方法

        List<Flight> flights = Arrays.asList(
                new Flight(1L, "CZ3104", "广州", "北京"),
                new Flight(2L, "CA1357", "上海", "成都")
        );
        model.addAttribute("flights", flights);

        return "notificationmanagement";
    }

}
